﻿using System;
using System.Collections.Generic;

namespace StockTrader.Core
{
    public interface IBroker : IDisposable
    {
        // Login methods
        BrokerErrorCode LogIn();
        void LogOut();
        BrokerErrorCode IsLoggedIn();
        BrokerErrorCode CheckAndLogInIfNeeded(bool bForceLogin, bool bCheckRemoteControl = false);

        // Common Equity and Derivative methods
        BrokerErrorCode GetOrderStatus(string orderNumber,
            InstrumentType instrumentType,
            DateTime fromDate,
            DateTime toDate,
            out OrderStatus orderStatus);
        
        // Gets the trade execution price of the given instrument type
        double GetTradeExecutionPrice(DateTime fromDate,
            DateTime toDate,
            InstrumentType instrumentType,
            string orderRefString);

        // Derivatives 
        BrokerErrorCode GetDerivativesTradeBook(DateTime fromDate,
            DateTime toDate,
            InstrumentType instrumentType,
            bool newTradesOnly,
            out Dictionary<string, DerivativeTradeBookRecord> trades);

        BrokerErrorCode GetDerivativesOrderBook(DateTime fromDate,
            DateTime toDate,
            InstrumentType instrumentType,
            bool newTradesOnly,
            bool bOnlyOutstandingOrders,
            out Dictionary<string, DerivativeOrderBookRecord> orders);

        BrokerErrorCode PlaceDerivativeOrder(string stockCode,
            int quantity,
            double price,
            OrderPriceType orderType,
            OrderDirection orderDirection,
            InstrumentType instrumentType,
             double strikePrice,
            DateTime expiryDate,
            OrderGoodNessType orderGoodnessType,
            out string orderNumber);

        BrokerErrorCode GetDerivativeQuote(string underlyingSymbol,
            InstrumentType instrumentType,
            DateTime expiryDate,
            double strikePrice,
            out DerivativeSymbolQuote info);

        BrokerErrorCode GetDerivativeSpread(string underlyingSymbol,
          InstrumentType instrumentType,
          DateTime expiryDate,
          double strikePrice,
          out DerivativeSymbolSpread info);

        BrokerErrorCode CancelDerivativesOrder(string orderRef, bool refreshOrderBook);

        // Equity methods
        List<BrokerErrorCode> PlaceMultipleEquityOrders(List<EquityOrderRecord> orders, int maxRetries);
        
        BrokerErrorCode GetEquityQuote(string stockCode, out EquitySymbolQuote[] info);
        BrokerErrorCode GetEquitySpread(string stockCode, out EquitySymbolSpread[] info);

        BrokerErrorCode PlaceEquityMarginPLUSOrder(string stockCode,
            int quantity,
            string stopLossPrice,
            string limitPrice,
            OrderDirection orderDirection);

        BrokerErrorCode PlaceEquityMarginOrder(string stockCode,
            int quantity,
            string stopLossPrice,
            string limitPrice,
            OrderDirection orderDirection);

        BrokerErrorCode PlaceEquityDeliveryOrder(string stockCode,
            int quantity,
            string price,
            OrderPriceType orderPriceType,
            OrderDirection orderDirection,
            Exchange exchange);

        BrokerErrorCode PlaceEquityMarginDeliveryFBSOrder(string stockCode,
            int quantity,
            string price,
            OrderPriceType orderPriceType,
            OrderDirection orderDirection,
            EquityOrderType orderType,
            Exchange exchange);

        BrokerErrorCode GetEquityTradeBook(DateTime fromDate,
            DateTime toDate,
            bool newTradesOnly,
            string stockCode,
            out Dictionary<string, EquityTradeBookRecord> trades);

        BrokerErrorCode GetEquityOrderBook(DateTime fromDate,
            DateTime toDate,
            bool newTradesOnly,
            bool bOnlyOutstandingOrders,
            string stockCode,
            out Dictionary<string, EquityOrderBookRecord> orders);

        BrokerErrorCode CancelEquityOrder(string orderRef);
        
        BrokerErrorCode CancelAllOutstandingEquityOrders(string stockCode,
            out Dictionary<string, BrokerErrorCode> cancelOrderErrCodes);

    }
}
